const express=require('express');
const goods=express.Router();
const axios = require('axios')
const jwt = require("jsonwebtoken")//解析token

// 跳转到商品列表页面
goods.get('/goodsMenu',(req, res)=>{
    res.render('admin/goodsMenu',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 查询所有商品
goods.get('/ManageAllGoods', (req, res) => {
    let {page,size} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/ManageAllGoods',
        method: 'GET',
        params: {
            page: page,
           size: size
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 模糊查询商品
goods.get('/findGoodsByName', (req, res) => {
    let {page,size,goodsName } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findGoodsByName',
        method: 'GET',
        params: {
            page: page,
            size: size,
            goodsName :goodsName  
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 根据时间查询商品
goods.get('/findByTime', (req, res) => {
    let {page,size,time  } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findGoodsByTime',
        method: 'GET',
        params: {
            page: page,
            size: size,
            time :time  
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 删除单个商品
goods.delete('/deleteAGood', (req, res) => {
    let {goodsId } = req.query;
    console.log("-------------");
    console.log(req.query);
    axios({
        url: 'consumer/deleteAGood',
        method: 'DELETE',
        params: {
            goodsId:goodsId ,
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 批量删除商品
goods.delete('/deleteGoods', (req, res) => {
    let {ids} = req.query;
    console.log("-------");
    console.log(req.query);
    let delid="";
    for(let key of ids){
        delid = delid+','+key
    }
    console.log(delid);
    axios({
        url: 'consumer/deleteGoods ',
        method: 'DELETE',
        params: {
            ids:delid
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 根据是否上架查询商品
goods.get('/findGoodsByFlag', (req, res) => {
    let {page,size,flag} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findGoodsByFlag',
        method: 'GET',
        params: {
            page: page,
            size: size,
            flag:flag
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})

// 通过商品的的上架申请
goods.patch('/passAGood', (req, res) => {
    let {goodsId} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/passAGood',
        method: 'PATCH',
        params: {
            goodsId :goodsId 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 拒绝商品的的上架申请
goods.post('/refuseAGood', (req, res) => {
    let {goodsId ,userId ,comment } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/refuseAGood',
        method: 'POST',
        params: {
            goodsId:goodsId,
            adminId :jwt.decode(req.session.token).id,
            comment :comment ,
            userId :userId 
            }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})

// 跳转至商品详情
goods.get('/goodsDetail',(req, res)=>{
    let {goodsId,userId} = req.query
   console.log(req.query);
    axios({
        url: 'consumer/findAGoodById',
        method: 'get',
        params: {
            goodsId:goodsId,
            }
    })
    .then(data => {
        console.log(data.data);
        console.log(goodsId);
        res.render('admin/goodsDetail',{
            info:data.data.data,
            userInfo: jwt.decode(req.session.token),
            goodsId:goodsId,
            userId:userId
        });
    })
    .catch(err => {
        console.log(err.config);
        if(err.response.status==500){
            req.session.error={
                status:err.response.status,
                msg:"很抱歉服务器出现故障，工作人员正在努力修复"
            }
        }else if(err.response.status==404){
            req.session.error={
                status:err.response.status, 
                msg:"你当前访问的页面不存在"
            }
        }
        res.redirect('/admin/error')
    })
    
});

// 跳转到品牌管理
goods.get('/brandMenu',(req, res)=>{
    res.render('admin/brandMenu');
});



// 跳转到分类管理
goods.get('/sortMenu',(req, res)=>{
    res.render('admin/sortMenu',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 查找一级分类
goods.get('/findFirst',(req, res)=>{
    axios({
        url: 'consumer/findFirst',
        method: 'GET',
        params: {
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
        cateData= data.data.data
        const arr = []
        cateData.forEach((item, index) => {
            const {homeType} = item
            if (!goodsData[index].length) return
            let obj = {
                homeType,
                data: goodsData[index]
            }
            arr.push(obj)
        });
        console.log(arr)
        res.render('home/index',{arr})
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 通过一级分类id查看二级分类
goods.get('/findSecond',(req, res)=>{
    let {bigId} = req.query; 
    axios({
        url: 'consumer/findSecond',
        method: 'GET',
        params: {
            bigId:bigId,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 通过二级分类id查看三级分类
goods.get('/findThird',(req, res)=>{
    let {mediumId } = req.query;
    axios({
        url: 'consumer/findThird',
        method: 'GET',
        params: {
            mediumId:mediumId,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 通过三级分类id查看四级分类
goods.get('/findFour',(req, res)=>{
    let {littleId } = req.query;
    axios({
        url: 'consumer/findFour',
        method: 'GET',
        params: {
            littleId:littleId,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 添加一个一级分类
goods.post('/addFirst',(req, res)=>{
    let {bType} = req.query;
    axios({
        url: 'consumer/addFirst',
        method: 'POST',
        params: {
            bType :bType ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 添加一个二级级分类
goods.post('/addSecond',(req, res)=>{
    let {mType ,bigId} = req.query;
    axios({
        url: 'consumer/addSecond',
        method: 'POST',
        params: {
            mType  :mType ,
            bigId:bigId ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 添加一个三级分类
goods.post('/addThird',(req, res)=>{
    let {lType ,mediumId } = req.query;
    axios({
        url: 'consumer/addThird',
        method: 'POST',
        params: {
            lType  :lType ,
            mediumId :mediumId  ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => { 
        console.log(err.config);
    })
});
// 添加一个四级分类
goods.post('/addFour',(req, res)=>{
    let {littleId,brandName  } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/addFour',
        method: 'POST',
        params: {
            littleId :littleId,
            brandName:brandName ,
            id:jwt.decode(req.session.token).id   
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => { 
        console.log(err.config);
    })
});
// 删除一个一级分类
goods.delete('/deleteFirst',(req, res)=>{
    let {bigId} = req.query;
    axios({
        url: 'consumer/deleteFirst',
        method: 'DELETE',
        params: {
            bigId:bigId,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 删除一个二级分类
goods.delete('/deleteSecond',(req, res)=>{
    let {mediumId } = req.query;
    axios({
        url: 'consumer/deleteSecond',
        method: 'DELETE',
        params: {
            mediumId :mediumId ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 删除一个三级分类
goods.delete('/deleteThird',(req, res)=>{
    let {littleId } = req.query;
    axios({
        url: 'consumer/deleteThird',
        method: 'DELETE',
        params: {
            littleId :littleId ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 删除一个四级分类
goods.delete('/deleteFour',(req, res)=>{
    let {brandId } = req.query;
    axios({
        url: 'consumer/deleteFour',
        method: 'DELETE',
        params: {
            brandId :brandId ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 修改一个一级分类
goods.put('/updateFirst',(req, res)=>{
    let {bigId,bType} = req.query;
    axios({
        url: 'consumer/updateFirst',
        method: 'PUT',
        params: {
            bigId:bigId,
            bType :bType ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 修改一个二级分类
goods.put('/updateSecond',(req, res)=>{
    let {mediumId,mType} = req.query;
    axios({
        url: 'consumer/updateSecond',
        method: 'PUT',
        params: {
            mediumId:mediumId,
            mType :mType ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 修改一个三级分类
goods.put('/updateThird',(req, res)=>{
    let {littleId,lType} = req.query;
    axios({
        url: 'consumer/updateThird',
        method: 'PUT',
        params: {
            littleId:littleId,
            lType :lType ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});
// 修改一个四级分类
goods.put('/updateFour',(req, res)=>{
    let {brandName ,brandId} = req.query;
    axios({
        url: 'consumer/updateFour',
        method: 'PUT',
        params: {
            brandName :brandName ,
            brandId :brandId ,
            id:jwt.decode(req.session.token).id
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
});




module.exports=goods;



